package com.wlcorp.forcazamis.dao.stock.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.stock.IStockDAO;
import com.wlcorp.forcazamis.domain.stock.Stock;

public class StockDAOImpl extends HibernateDaoSupport implements IStockDAO {

	public void deleteStock(Stock stock) throws DaoException {
		stock.setDeleteFlag(true);
		getHibernateTemplate().update(stock);
	}

	public Stock getStockById(Long stockId) throws DaoException {
		return (Stock)getHibernateTemplate().get(Stock.class,stockId);
	}

	@SuppressWarnings("unchecked")
	public List getStockList(final Stock stock) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select stock from Stock as stock where 1=1 and stock.deleteFlag=false");
		if(null!=stock&&null!=stock.getStockCode()){
			queryString.append(" and stock.stockCode like '%").append(stock.getStockCode()).append("%'");
		}
		queryString.append(" order by stock.stockDate desc");
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != stock && null != stock.getMinRownum()){
					query.setFirstResult(stock.getMinRownum());
					query.setMaxResults(stock.getNumPerPage());
				}
				return query.list();
			}
		});
	}

	public void saveStock(Stock stock) throws DaoException {
		getHibernateTemplate().flush();
		getHibernateTemplate().clear();
		getHibernateTemplate().saveOrUpdate(stock);
	}
	
	public void updateStock(Stock stock) throws DaoException {
		getHibernateTemplate().update(stock);
	}

	public Stock getStockByStockCodeAndFactoryId(String stockCode, Long factoryId) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select stock from Stock as stock where stock.deleteFlag=false and ")
		.append(" stock.stockCode = '").append(stockCode).append("'").append(" and stock.factory.factoryId=").append(factoryId);
		return (Stock)getHibernateTemplate().execute(new HibernateCallback(){
		public Object doInHibernate(Session session)
				throws HibernateException, SQLException {
			Query query = session.createQuery(queryString.toString());
			return query.uniqueResult();
		}
	});
	}

	public Long getMaxStockId() throws DaoException {
		 String hql="select max(stock.stockId) from Stock stock";
		 return ((Long)getHibernateTemplate().iterate(hql).next()).longValue();
	}

	public Long getStockCount(final Stock stock) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select count(stock) from Stock as stock where stock.deleteFlag=false");
		if(null != stock.getStockCode()){
			queryString.append(" and stock.stockCode = '").append(stock.getStockCode()).append("'");
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != stock && null != stock.getMinRownum()){
					query.setFirstResult(stock.getMinRownum());
					query.setMaxResults(stock.getNumPerPage());
				}
				return query.uniqueResult();
			}
			
		});
	}
}
